package com.tjvib.service;

import android.content.Context;
import android.os.ResultReceiver;

import com.tjvib.common.Callback;
import com.tjvib.sensor.LocalSensor;
import com.tjvib.sensor.LocalSensorData;
import com.tjvib.sensor.Sensor;
import com.tjvib.util.LogUtil;
import com.tjvib.util.ToastUtil;

public class LocalSensorService extends SensorService<LocalSensorData> {

    public static final int MSG_SENSOR_CONNECTED = 6;
    public static final int MSG_SENSOR_DISCONNECTED = 7;
    public static final int MSG_SENSOR_CONNECTION_ERROR = 8;
    public static final String KEY_MESG = "MESG";

    Context context = this;

    public static boolean isRunning = false;
    ResultReceiver uiMessageReceiver;

    LocalSensor localSensor = LocalSensor.getInstance(context);

    @Override
    public void connectSensor() {
        if (localSensor.getConnectionStatus() == Sensor.SENSOR_STATUS_CONNECTED) {
            //sendMessageToUI(KEY_MESG, "Local sensor has already been connected", MSG_SENSOR_CONNECTION_ERROR);
            LogUtil.e("Local sensor has already been connected");
            return;
        }

            /*new Thread(new Runnable() {
                @Override
                public void run() {*/
        if (!localSensor.connect()) {
            LogUtil.e("Failed to connect to local sensor");
            //sendMessageToUI(KEY_MESG, "Failed to connect to local sensor", MSG_SENSOR_CONNECTION_ERROR);
        }
              /*  }
            }).start();*/
    }

    @Override
    void connectSensor(String address, Callback callback) {

    }


    @Override
    public void disconnectSensor() {
        if (localSensor.getConnectionStatus() == Sensor.SENSOR_STATUS_DISCONNECTED) {
            LogUtil.i("Local sensor has already been disconnected");
            //sendMessageToUI(KEY_MESG, "Local sensor has already been disconnected", MSG_SENSOR_DISCONNECTED);
            return;
        }
        localSensor.disconnect();
    }

    @Override
    void disconnectSensor(String address, Callback callback) {

    }

    @Override
    public void disconnectAllSensor() {
        disconnectSensor();
    }

    @Override
    void disconnectAllSensor(Callback callback) {

    }

    @Override
    public int getSensorConnectionStatus() {
        return localSensor.getConnectionStatus();
    }

    @Override
    int getSensorConnectionStatus(String address) {
        return 0;
    }

    @Override
    public void setStreamFrequency(int freq) {
        localSensor.setStreamFrequency(freq);
    }

    @Override
    void setStreamFrequency(String address, int freq) {

    }

    @Override
    public int getStreamFrequency() {
        return localSensor.getStreamFrequency();
    }

    @Override
    int getStreamFrequency(String address) {
        return 0;
    }

    @Override
    public void resetTimestamp() {
        if(!localSensor.isRecording())
            localSensor.resetTimestamp();
        else
            ToastUtil.show("结束记录后才能重新设置timestamp");
    }

    @Override
    public void resetTimestamp(String address) {
        localSensor.resetTimestamp();
    }

    @Override
    public LocalSensorData getData() {
        return localSensor.getData();
    }

    @Override
    LocalSensorData getData(String address) {
        return null;
    }

    @Override
    public void startRecording() {
        localSensor.startRecording(context);
    }

    @Override
    public void stopRecording() {
        localSensor.stopRecording();
    }

    @Override
    public boolean isRecording() {
        return localSensor.isRecording();
    }

    @Override
    public void discardData() {
        localSensor.discardData();
    }

    @Override
    public void storeData(String name, Callback callback) {
        localSensor.storeData(name, callback);
    }
}
